package com.rul.mars.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.rul.mars.common.constant.DeleteStatusConstant;
import com.rul.mars.common.constant.ReceiveAddressDefaultStatusConstant;
import com.rul.mars.common.exception.Asserts;
import com.rul.mars.mbg.domain.ReceiveAddressDetail;
import com.rul.mars.mbg.mapper.ReceiveAddressMapper;
import com.rul.mars.mbg.model.ReceiveAddress;
import com.rul.mars.mbg.model.ReceiveAddressExample;
import com.rul.mars.mbg.model.User;
import com.rul.mars.user.service.UserAddressService;
import com.rul.mars.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户收货地址服务实现
 *
 * @author RuL
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private ReceiveAddressMapper receiveAddressMapper;

    @Autowired
    private UserService userService;

    /**
     * 收货地址详细信息
     *
     * @param id 收货地址ID
     * @return 地址详细信息
     */
    @Override
    public ReceiveAddressDetail detail(Long id) {
        ReceiveAddress receiveAddress = receiveAddressMapper.selectByPrimaryKey(id);
        return BeanUtil.copyProperties(receiveAddress, ReceiveAddressDetail.class);
    }

    /**
     * 查询用户的所有收货地址
     *
     * @return 用户的所有收货地址
     */
    @Override
    public List<ReceiveAddressDetail> listAddressDetail() {
        User user = userService.currentUser();

        ReceiveAddressExample example = new ReceiveAddressExample();
        example.createCriteria()
                .andUserIdEqualTo(user.getId())
                .andDeleteStatusEqualTo(DeleteStatusConstant.NO);
        List<ReceiveAddress> receiveAddresses = receiveAddressMapper.selectByExample(example);

        return receiveAddresses.stream()
                .map(item -> BeanUtil.copyProperties(item, ReceiveAddressDetail.class))
                .collect(Collectors.toList());
    }

    /**
     * 删除收货地址
     *
     * @param id 收货地址ID
     * @return 删除结果
     */
    @Override
    public boolean deleteAddress(Long id) {
        ReceiveAddress address = receiveAddressMapper.selectByPrimaryKey(id);
        if (address.getDefaultStatus() == DeleteStatusConstant.YES) {
            //该地址为默认地址
            Asserts.fail("默认地址不能删除");
        }
        //设置收货地址为删除状态
        address.setDeleteStatus(DeleteStatusConstant.YES);
        return receiveAddressMapper.updateByPrimaryKeySelective(address) == 1;
    }

    /**
     * 修改收货地址信息
     *
     * @param detail 需修改的收货地址信息
     * @return 修改后的收货地址信息
     */
    @Override
    public boolean updateAddress(ReceiveAddressDetail detail) {
        ReceiveAddress address = BeanUtil.copyProperties(detail, ReceiveAddress.class);
        User user = userService.currentUser();

        if (address.getDefaultStatus() == DeleteStatusConstant.YES) {
            //默认地址状态为YES,取消其他默认收货地址
            cancelDefaultStatus(user.getId());
        }
        return receiveAddressMapper.updateByPrimaryKeySelective(address) == 1;
    }

    /**
     * 将地址ID为id的地址设置默认地址
     *
     * @param id 地址ID
     */
    @Override
    public void setAsDefault(Long id) {
        User user = userService.currentUser();
        //取消其他默认收货地址
        cancelDefaultStatus(user.getId());

        //设置为默认地址
        ReceiveAddress address = getById(id);
        address.setDefaultStatus(ReceiveAddressDefaultStatusConstant.YES);
        receiveAddressMapper.updateByPrimaryKeySelective(address);
    }

    /**
     * 添加收货地址
     *
     * @param detail 需添加的地址信息
     */
    @Override
    public void addReceiveAddress(ReceiveAddressDetail detail) {
        ReceiveAddress address = BeanUtil.copyProperties(detail, ReceiveAddress.class);
        User user = userService.currentUser();

        if (address.getDefaultStatus() == DeleteStatusConstant.YES) {
            //设为默认收货地址，取消其他的默认地址
            cancelDefaultStatus(user.getId());
        }
        //添加新的收货地址
        receiveAddressMapper.insertSelective(address);
    }

    /**
     * 获取用户的默认地址信息
     *
     * @param userId 用户ID
     * @return 该用户的默认地址信息
     */
    public ReceiveAddress getDefaultAddress(Long userId) {
        ReceiveAddressExample example = new ReceiveAddressExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andDefaultStatusEqualTo(ReceiveAddressDefaultStatusConstant.YES);

        List<ReceiveAddress> addressList = receiveAddressMapper.selectByExample(example);
        if (CollUtil.isEmpty(addressList)) {
            return null;
        }
        return addressList.get(0);
    }

    /**
     * 取消用户的默认地址状态
     *
     * @param userId 用户ID
     */
    public void cancelDefaultStatus(Long userId) {
        ReceiveAddress defaultAddress = getDefaultAddress(userId);
        if (defaultAddress != null) {
            defaultAddress.setDefaultStatus(DeleteStatusConstant.NO);
            receiveAddressMapper.updateByPrimaryKeySelective(defaultAddress);
        }
    }

    /**
     * 根据ID查询地址信息
     *
     * @param id 地址ID
     * @return 地址信息
     */
    public ReceiveAddress getById(Long id) {
        return receiveAddressMapper.selectByPrimaryKey(id);
    }
}
